package com.teamwork.module01.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teamwork.common.entity.module01.Class;
import com.teamwork.common.entity.module01.Course;
import com.teamwork.common.entity.module01.Student;
import com.teamwork.common.result.Result;
import com.teamwork.module01.service.CourseService;
import com.teamwork.module01.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/mod01/student")
@Slf4j
public class StudentController {

    @Resource
    private StudentService studentService;
    @Resource
    private CourseService courseService;

    @GetMapping("/getbystuid")
    public Result getByStuId(@RequestParam("stu_id") Long stuid){
        //log.info("{}",stuid);
        return studentService.getByStuId(stuid);
    }



    @GetMapping("/course/{sid}")
    public Result getCourses(@PathVariable("sid") Long id){

        List<Course> courses = courseService.getoneCourse(id);

        String s = (String) JSONObject.toJSON(courses);

        return Result.success(s);
    }


    @GetMapping("/getbyname")
    public Result getByname(String name){
       // log.info(name);

        return studentService.getByName(name);
    }
    @GetMapping("/page")
    public Result page(Integer page,Integer size){
        return studentService.getPage(page,size);
    }
    @PostMapping
    public Result InsertOrUpdate(@RequestBody Student student){
        //log.info("{}",student);
        return studentService.InsertOrUpdate(student);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        return studentService.deleteById(id);
    }
    @DeleteMapping
    public Result batchDelete(@RequestBody Map<String,Long[]> map){
        Long[] ids = map.get("ids");
        List<Long> idList = Arrays.asList(ids);

        return studentService.batchDelete(idList);
    }

}
